package com.reggie.module.controller;

import com.reggie.common.ResultResponseVo;
import com.reggie.module.entity.po.ShoppingCart;
import com.reggie.module.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author 早睡蛋
 * @Date 2022/9/29 11:48:19
 * @Desc:
 */
@RestController
@RequestMapping("/shoppingCart")
@Slf4j
@CrossOrigin
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public ResultResponseVo add(@RequestBody ShoppingCart shoppingCart, HttpServletRequest request) {
        return shoppingCartService.add(shoppingCart, request);
    }

    /**
     * 购物车信息列表展示
     *
     * @return
     */
    @GetMapping("/list")
    public ResultResponseVo list(HttpServletRequest request) {
        return shoppingCartService.listByUserId(request);
    }

    /**
     * 购物车中减少商品
     * @param id
     * @return
     */
    @PostMapping("/sub")
    public ResultResponseVo sub(@RequestParam(value = "id") Long id){
        log.info("id:{}",id);
        return shoppingCartService.subById(id);
    }

    /**
     * 清空购物车
     * @param request
     * @return
     */
    @DeleteMapping("/clean")
    public ResultResponseVo clean(HttpServletRequest request){
        return shoppingCartService.cleanCart(request);
    }


}
